package com.yuyou.hxyy.gateway.security.filter;

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.yuyou.hxyy.service.api.bean.LoginRedisObj;
import com.yuyou.hxyy.service.api.security.SecurityCons;

public class AcessTokenAuthentication implements Authentication {

	private static final long serialVersionUID = 3730332217518791533L;

	private LoginRedisObj loginRedisObj;

	private Boolean authentication = false;

	public AcessTokenAuthentication(LoginRedisObj loginRedisObj,
			Boolean authentication) {
		this.loginRedisObj = loginRedisObj;
		this.authentication = authentication;
	}

	public LoginRedisObj getLoginRedisObj() {
		return loginRedisObj;
	}

	public void setLoginRedisObj(LoginRedisObj loginRedisObj) {
		this.loginRedisObj = loginRedisObj;
	}

	/**
	 * 权限验如（hasRole时候判断）
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return Stream
				.of(SecurityCons.role.getNameByType(loginRedisObj.getUserType()))
				.map(SimpleGrantedAuthority::new).collect(Collectors.toList());
	}

	@Override
	public Object getCredentials() {
		return "";
	}

	@Override
	public Object getDetails() {
		return loginRedisObj;
	}

	@Override
	public Object getPrincipal() {
		return loginRedisObj;
	}

	@Override
	public boolean isAuthenticated() {
		return authentication;
	}

	@Override
	public void setAuthenticated(boolean isAuthenticated)
			throws IllegalArgumentException {
		this.authentication = isAuthenticated;
	}

	@Override
	public String getName() {
		return loginRedisObj.getUsername();
	}
}
